package zmj.test.javaparser.visitor;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.GenericVisitorAdapter;
import zmj.test.javaparser.util.FindFileVisitor;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class VisitAST {
    public static void main(String[] args) throws IOException {
        File base = new File("");
        String sourcePath = "test-case/javaparser-testcase/src/main/java";

        FindFileVisitor findJavaVisitor = new FindFileVisitor(".java");
        Files.walkFileTree(Paths.get(base.getCanonicalPath() + File.separator + sourcePath), findJavaVisitor);
        for (String name : findJavaVisitor.getFileNameList()){
            ParseResult<CompilationUnit> result = new JavaParser().parse(Paths.get(name));
            result.getResult().ifPresent(cu -> new TestVisitor().visit(cu, null));
        }
    }
}

class TestVisitor extends GenericVisitorAdapter<Void, Void> {
    @Override
    public Void visit(MethodCallExpr n, Void arg) {
        System.out.println("function call: " + n.toString());
        return super.visit(n, arg);
    }

    @Override
    public Void visit(MethodDeclaration n, Void arg) {
        System.out.println("function declaration: " + n.getNameAsString());
        return super.visit(n, arg);
    }
}
